package com.glodon.paas.document.biz;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.glodon.paas.document.DocumentBaseIT;
import com.glodon.paas.document.constant.DocumentConstant;
import com.glodon.paas.document.dao.MultiPartUploadHandleDao;
import com.glodon.paas.document.dao.PartDao;
import com.glodon.paas.document.dao.RevisionDao;
import com.glodon.paas.document.dao.domain.FileInfoEntity;
import com.glodon.paas.document.dao.domain.RevisionEntity;
import com.glodon.paas.exception.NotFoundException;
import com.glodon.paas.exception.PaasServerException;
import com.glodon.paas.exception.PrivilegeException;
import com.glodon.paas.exception.ValidateException;
import com.glodon.paas.exception.i18n.ErrorCode;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = { "classpath:test-document-biz.xml", "classpath:paas-document-dao-mysql.xml", })
@Ignore
public class FileBizMySqlIT extends DocumentBaseIT {

    private static final String      revisionIdPre = "ThisIsTestRevisionId";

    private static final String      handlerIdPre  = "ThisIsTestHandlerId";

    private static final String      partIdPre     = "ThisIsTestPartId";

    private static final String      storagekey    = "ThisIsTestStorageKey";

    private FileInfoEntity           resultFile;

    @Autowired
    private RevisionDao              revisionDao;

    @Autowired
    private MultiPartUploadHandleDao handleDao;

    @Autowired
    private PartDao                  partDao;

    @Autowired
    private FileBiz                  fileBiz;

    @BeforeClass
    public static void beforeClassSetUp() throws Exception {

    }

    @Before
    public void setUp() {
        if (!initedData) {
            createFolder(1);
            createDoc(2);
            removeFile(3);
            fileBiz.insertFile(newFile(fileIdPre + 3, fileIdPre + 1, "ThisIsSubFile", "txt", ownerId,
                                       DocumentConstant.FILE_TYPE_FILE));
            initedData = true;
        }
    }

    public RevisionEntity newRevision(int idNum) {
        RevisionEntity revision = new RevisionEntity();
        revision.setFileId(fileIdPre + idNum);
        revision.setId(revisionIdPre + idNum);
        revision.setFileSize(fileSize);
        revision.setRevisionNumber(idNum);
        revision.setTimestamp(new Date());
        revision.setStorageKey(storagekey);
        return revision;
    }

    public void removeRevision(int idNum) {
        revisionDao.delete(revisionIdPre + idNum);
    }

    public void createRevision(int idNum) {
        revisionDao.insert(newRevision(idNum));
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
    }

    /**
     * Test method for
     * {@link com.glodon.paas.document.doc.fileBiz.FileService#createFolder(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}
     * .
     */
    @Test
    public void testGetFileCase1() throws PaasServerException {
        try {
            resultFile = fileBiz.getFile(null);
        } catch (ValidateException e) {
            assertEquals(ErrorCode.VALIDATE_ERROR, e.getErrorCode());
        }
        createDoc(1);
        assertNotNull(fileBiz.getFile(fileId1));
    }

    @Test
    public void testGetFileCase2() throws PaasServerException {
        assertNotNull(fileBiz.getFile(fileId1));
    }

    @Test
    public void testGetNormalFileCase1() throws PaasServerException {
        try {
            fileBiz.removeFile(fileId1);
            resultFile = fileBiz.getNormalFile(fileId1);
        } catch (NotFoundException e) {
            assertEquals(ErrorCode.FILE_BEEN_DELETED, e.getErrorCode());
        }
        fileBiz.restore(fileId1);
        assertNotNull(fileBiz.getNormalFile(fileId1));
    }

    @Test
    public void testGetNomalFileCase2() throws PaasServerException {
        assertNotNull(fileBiz.getNormalFile(fileId1));
    }

    @Test
    public void testGetDeletedFile() throws PaasServerException {
        try {
            resultFile = fileBiz.getDeletedFile("");
        } catch (ValidateException e) {
            assertEquals(ErrorCode.VALIDATE_ERROR, e.getErrorCode());
        }
        try {
            resultFile = fileBiz.getDeletedFile(fileId1);
        } catch (NotFoundException e) {
            assertEquals(ErrorCode.FILE_NOT_EXIST, e.getErrorCode());
        }
        fileBiz.removeFile(fileId1);
        assertNotNull(fileBiz.getDeletedFile(fileId1));
        fileBiz.restore(fileId1);
    }

    @Test
    public void testGetDeletedFiles() throws PaasServerException {
        try {
            files = fileBiz.getDeletedFiles("");
        } catch (ValidateException e) {
            assertEquals(ErrorCode.VALIDATE_ERROR, e.getErrorCode());
        }
        fileBiz.removeFile(fileId1);
        assertTrue(fileBiz.getDeletedFiles(ownerId).size() > 0);
        fileBiz.restore(fileId1);
    }

    @Test
    public void testGetFileByPath() throws PaasServerException {
        try {
            fileBiz.getFileId(ownerId, "aa/bb/cc", new Integer[] { DocumentConstant.FILE_STATUS_NORMAL });
        } catch (PaasServerException e) {
            assertEquals(e.getErrorCode(), ErrorCode.FILE_NOT_EXIST);
        }
    }

    @Test
    public void testGetAvailableRootFiles() throws PaasServerException {
        assertNotNull(fileBiz.getFiles(ownerId, null, new Integer[] { DocumentConstant.FILE_STATUS_NORMAL }, null));
    }

    @Test
    public void testGetAvailableSubFiles() throws PaasServerException {
        fileBiz.removeFile(fileIdPre + 3);
        List<FileInfoEntity> files = fileBiz.getFiles(fileId1, null,
                                                      new Integer[] { DocumentConstant.FILE_STATUS_NORMAL }, null);
        assertTrue(files == null || files.size() == 0);
    }

    @Test
    public void testGetAllSubFiles() throws PaasServerException {
        assertNotNull(fileBiz.getFiles(fileId1, null, null, null));
    }

    @Test
    public void testGetParents() throws PaasServerException {
        assertNotNull(fileBiz.getParents(files.get(2)));
    }

    @Test
    public void testUploadFileName() throws PaasServerException {
        fileBiz.updateFileName(fileId1, "newFileName", ownerId);
    }

    @Test
    public void testGetUsedSpace() throws PaasServerException {
        assertTrue(fileBiz.getUsedSpace(ownerId) > 0);
    }

    @Test
    public void testUpdateShareStatus() throws PaasServerException {
        fileBiz.shareFile(fileId1);
    }

    @Test
    public void removeData() {
        if (initedData) {
            cleanFile();
            initedData = false;
        }
    }
}
